package com.shop2cn.app.liveseller.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.app.liveseller.parameter.*;
import com.shop2cn.app.liveseller.service.*;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.business.service.LiveRoomShop2cnCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.livesellerShop2cn.*;
import com.ymttest.database.sqlwapper.livesellerShop2cn.LiveRoomShop2cnWapper;
import com.ymttest.utils.TestDataManager;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;

/**
 * 获取待发优惠券列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetCouponList {
	private static GetCouponListBean getcouponlistBean;
	private static GetCouponListCall getcouponlistCall;
	private static int sellerId;
	private static String token;
	private static int csId;
	private static String cstoken;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取待发优惠券列表");
		try {
			sellerId = GodmchManageCallService.CreateSellerMch();
			token=AuthCallServiceV2.createToken(sellerId, "app");
			csId = GodmchManageCallService.bindClientService(
					sellerId, 1);
			cstoken = AuthCallServiceV2.createToken(csId, "app");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		getcouponlistBean = new GetCouponListBean();
		getcouponlistCall = new GetCouponListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcouponlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCouponList_001() {
		Logger.start(true, "获取待发优惠券列表");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setSellerId(sellerId);
			sellercreatebatchBean.setCurrency("USD");
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			//sellerCreateBatchCall.getBatchCode();
			getcouponlistBean.setPageIndex(1);
			getcouponlistBean.setPageSize(10);
			String head="accesstoken="+token;
			Logger.debug("ymt-pars:"+ head);
			getcouponlistCall.addHeader("ymt-pars", head);
			getcouponlistBean.setLiveId(createlive.getId());
			getcouponlistCall.setData(getcouponlistBean);
			getcouponlistCall.callService();
			Logger.verifyEquals(true, Integer.valueOf(getcouponlistCall.getString("total"))>0, "验证有优惠券");
			JsonArray jas = getcouponlistCall.getJsonObject().get("couponList").getAsJsonArray();
			JsonObject coupon = jas.get(0).getAsJsonObject();
			Logger.verifyEquals(sellerCreateBatchCall.getBatchCode(), coupon.get("batchCode").getAsString(), "验证优惠券");
			Logger.verifyEquals(sellercreatebatchBean.getBatchName(), coupon.get("batchName").getAsString(), "验证batchName");
			Logger.verifyEquals(sellercreatebatchBean.getEffectiveStartDate().getTime()/1000, coupon.get("effectiveStartTime").getAsLong()/1000, "验证effectiveStartTime");
			//结束时间是当天的23 59 59
			sellercreatebatchBean.getEffectiveEndDate().setHours(23);
			sellercreatebatchBean.getEffectiveEndDate().setMinutes(59);
			sellercreatebatchBean.getEffectiveEndDate().setSeconds(59);
			Logger.verifyEquals(sellercreatebatchBean.getEffectiveEndDate().getTime()/1000, coupon.get("effectiveEndTime").getAsLong()/1000, "验证effectiveEndTime");
			Logger.verifyEquals(sellercreatebatchBean.getCouponTotalCount(), coupon.get("couponTotalCount").getAsInt(), "验证couponTotalCount");
			Logger.verifyEquals(sellercreatebatchBean.getOrderMinAmount().setScale(2), new BigDecimal(coupon.get("orderMinAmount").getAsString()).setScale(2), "验证orderMinAmount");
			Logger.verifyEquals(sellercreatebatchBean.getCouponAmount().setScale(2), new BigDecimal(coupon.get("couponAmount").getAsString()).setScale(2), "验证couponAmount");
			//sendStatus	int	1- 发放中，2- 不在发放中
			Logger.verifyEquals(2, coupon.get("sendStatus").getAsInt(), "验证sendStatus");
			Logger.verifyEquals(sellercreatebatchBean.getIsShowInPage(), coupon.get("showOnPage").getAsBoolean(), "验证showOnPage");
			//币种校验 现在固定
			Logger.verifyEquals(sellercreatebatchBean.getCurrency(), coupon.get("currency").getAsJsonObject().get("code").getAsString(), "验证currency");
			Logger.verifyEquals("美元", coupon.get("currency").getAsJsonObject().get("name").getAsString(), "验证name");
			Logger.verifyEquals("$", coupon.get("currency").getAsJsonObject().get("symbol").getAsString(), "验证symbol");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCouponList_002() {
		Logger.start(true, "获取待发优惠券列表-已发放");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setSellerId(sellerId);
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			LiveRoomShop2cnCallService.SendCoupon(createlive.getId(), sellerCreateBatchCall.getBatchCode(), sellerId);
			Thread.sleep(2000);
			getcouponlistBean.setLiveId(createlive.getId());
			getcouponlistBean.setPageIndex(1);
			getcouponlistBean.setPageSize(10);
			String head="accesstoken="+token;
			Logger.debug("ymt-pars:"+ head);
			getcouponlistCall.addHeader("ymt-pars", head);
			getcouponlistCall.setData(getcouponlistBean);
			getcouponlistCall.callService();
			Logger.verifyEquals(true, Integer.valueOf(getcouponlistCall.getString("total"))>0, "验证有优惠券");
			JsonArray jas = getcouponlistCall.getJsonObject().get("couponList").getAsJsonArray();
			JsonObject coupon = jas.get(0).getAsJsonObject();
			Logger.verifyEquals(sellerCreateBatchCall.getBatchCode(), coupon.get("batchCode").getAsString(), "验证优惠券");
			Logger.verifyEquals(sellercreatebatchBean.getBatchName(), coupon.get("batchName").getAsString(), "验证batchName");
			Logger.verifyEquals(sellercreatebatchBean.getEffectiveStartDate().getTime()/1000, coupon.get("effectiveStartTime").getAsLong()/1000, "验证effectiveStartTime");
			//结束时间是当天的23 59 59
			sellercreatebatchBean.getEffectiveEndDate().setHours(23);
			sellercreatebatchBean.getEffectiveEndDate().setMinutes(59);
			sellercreatebatchBean.getEffectiveEndDate().setSeconds(59);
			Logger.verifyEquals(sellercreatebatchBean.getEffectiveEndDate().getTime()/1000, coupon.get("effectiveEndTime").getAsLong()/1000, "验证effectiveEndTime");
			Logger.verifyEquals(sellercreatebatchBean.getCouponTotalCount(), coupon.get("couponTotalCount").getAsInt(), "验证couponTotalCount");
			Logger.verifyEquals(sellercreatebatchBean.getOrderMinAmount().setScale(2), new BigDecimal(coupon.get("orderMinAmount").getAsString()).setScale(2), "验证orderMinAmount");
			Logger.verifyEquals(sellercreatebatchBean.getCouponAmount().setScale(2), new BigDecimal(coupon.get("couponAmount").getAsString()).setScale(2), "验证couponAmount");
			//sendStatus	int	1- 发放中，2- 不在发放中
			Logger.verifyEquals(1, coupon.get("sendStatus").getAsInt(), "验证sendStatus");
			Logger.verifyEquals(sellercreatebatchBean.getIsShowInPage(), coupon.get("showOnPage").getAsBoolean(), "验证showOnPage");
			Logger.verifyEquals(sellercreatebatchBean.getCurrency(), coupon.get("currency").getAsJsonObject().get("code").getAsString(), "验证currency");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCouponList_003() {
		Logger.start(true, "客服获取待发优惠券列表");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setSellerId(sellerId);
			sellercreatebatchBean.setCurrency("USD");
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			//sellerCreateBatchCall.getBatchCode();
			getcouponlistBean.setLiveId(createlive.getId());
			getcouponlistBean.setPageIndex(1);
			getcouponlistBean.setPageSize(10);
			String head="accesstoken="+cstoken;
			Logger.debug("ymt-pars:"+ head);
			getcouponlistCall.addHeader("ymt-pars", head);
			getcouponlistCall.setData(getcouponlistBean);
			getcouponlistCall.callService();
			Logger.verifyEquals(true, Integer.valueOf(getcouponlistCall.getString("total"))>0, "验证有优惠券");
			JsonArray jas = getcouponlistCall.getJsonObject().get("couponList").getAsJsonArray();
			JsonObject coupon = jas.get(0).getAsJsonObject();
			Logger.verifyEquals(sellerCreateBatchCall.getBatchCode(), coupon.get("batchCode").getAsString(), "验证优惠券");
			Logger.verifyEquals(sellercreatebatchBean.getBatchName(), coupon.get("batchName").getAsString(), "验证batchName");
			Logger.verifyEquals(sellercreatebatchBean.getEffectiveStartDate().getTime()/1000, coupon.get("effectiveStartTime").getAsLong()/1000, "验证effectiveStartTime");
			//结束时间是当天的23 59 59
			sellercreatebatchBean.getEffectiveEndDate().setHours(23);
			sellercreatebatchBean.getEffectiveEndDate().setMinutes(59);
			sellercreatebatchBean.getEffectiveEndDate().setSeconds(59);
			Logger.verifyEquals(sellercreatebatchBean.getEffectiveEndDate().getTime()/1000, coupon.get("effectiveEndTime").getAsLong()/1000, "验证effectiveEndTime");
			Logger.verifyEquals(sellercreatebatchBean.getCouponTotalCount(), coupon.get("couponTotalCount").getAsInt(), "验证couponTotalCount");
			Logger.verifyEquals(sellercreatebatchBean.getOrderMinAmount().setScale(2), new BigDecimal(coupon.get("orderMinAmount").getAsString()).setScale(2), "验证orderMinAmount");
			Logger.verifyEquals(sellercreatebatchBean.getCouponAmount().setScale(2), new BigDecimal(coupon.get("couponAmount").getAsString()).setScale(2), "验证couponAmount");
			//sendStatus	int	1- 发放中，2- 不在发放中
			Logger.verifyEquals(2, coupon.get("sendStatus").getAsInt(), "验证sendStatus");
			Logger.verifyEquals(sellercreatebatchBean.getIsShowInPage(), coupon.get("showOnPage").getAsBoolean(), "验证showOnPage");
			//币种校验 现在固定
			Logger.verifyEquals(sellercreatebatchBean.getCurrency(), coupon.get("currency").getAsJsonObject().get("code").getAsString(), "验证currency");
			Logger.verifyEquals("美元", coupon.get("currency").getAsJsonObject().get("name").getAsString(), "验证name");
			Logger.verifyEquals("$", coupon.get("currency").getAsJsonObject().get("symbol").getAsString(), "验证symbol");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCouponList_004() {
		Logger.start(true, "不输入liveid失败");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setSellerId(sellerId);
			sellercreatebatchBean.setCurrency("USD");
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			//sellerCreateBatchCall.getBatchCode();
			getcouponlistBean.setPageIndex(1);
			getcouponlistBean.setPageSize(10);
			String head="accesstoken="+token;
			Logger.debug("ymt-pars:"+ head);
			getcouponlistCall.addHeader("ymt-pars", head);
//			getcouponlistBean.setLiveId(createlive.getId());
			getcouponlistCall.setData(getcouponlistBean);
			getcouponlistCall.callService();
			Logger.verifyEquals("400", getcouponlistCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}